package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.AuthContextUtil;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "首页接口")
@RequestMapping("/admin/system/index")
//@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class IndexController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.OK(loginVo);
    }

    @GetMapping("/userinfo")
    @Operation(summary = "用户信息")
//    public Result<SysUser> getUserInfo(@RequestHeader("token") String token) {
    public Result<SysUser> getUserInfo() {
//        SysUser sysUser = sysUserService.getUserInfo(token);
        SysUser sysUser = AuthContextUtil.get();
        return Result.OK(sysUser);
    }

    @GetMapping("/generateValidateCode")
    @Operation(summary = "获取验证码")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.OK(validateCodeVo);
    }
    @GetMapping("/logout")
    @Operation(summary = "用户退出")
    @Parameters({@Parameter(name = "token", description = "令牌参数", required = true)
    })
    public Result logout(@RequestHeader("token") String token) {
        sysUserService.logout(token);
        return Result.OK(null);
    }

    @GetMapping("/menus")
    @Operation(summary = "查看菜单列表")
    public Result menus(){
        List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList();
        return Result.OK(sysMenuVoList);
    }

}
